package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.OrderDeliveryAddress;
import com.ericom.service.GenericManager;

public class OrderDeliveryAddressAction extends BaseAction {
	private GenericManager<OrderDeliveryAddress, Long> orderDeliveryAddressManager;
	private List orderDeliveryAddresss;
	private OrderDeliveryAddress orderDeliveryAddress;
	private Long id;

	public OrderDeliveryAddress getOrderDeliveryAddress() {
		return orderDeliveryAddress;
	}

	public void setOrderDeliveryAddress(
			OrderDeliveryAddress orderDeliveryAddress) {
		this.orderDeliveryAddress = orderDeliveryAddress;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setOrderDeliveryAddressManager(
			GenericManager<OrderDeliveryAddress, Long> orderDeliveryAddressManager) {
		this.orderDeliveryAddressManager = orderDeliveryAddressManager;
	}

	public String list() {
		orderDeliveryAddresss = orderDeliveryAddressManager.getAll();
		return SUCCESS;
	}

	public List getOrderDeliveryAddresss() {
		return orderDeliveryAddresss;
	}

	public String delete() {
		orderDeliveryAddressManager.remove(orderDeliveryAddress.getId());
		this.saveMessage(getText("orderDeliveryAddress.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			orderDeliveryAddress = orderDeliveryAddressManager.get(id);
		} else {
			orderDeliveryAddress = new OrderDeliveryAddress();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (orderDeliveryAddress.getId() == null);
		orderDeliveryAddress = orderDeliveryAddressManager
				.save(orderDeliveryAddress);

		String key = (isNew) ? "orderDeliveryAddress.added"
				: "orderDeliveryAddress.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
